<template>
<view class="container">
  <view class="order-info">
      <view class="t">
        <text class="lable">收货人：</text>
        <text class="value">{{orderInfo.consignee}} {{orderInfo.mobile}}</text>
      </view>
      <view class="b">{{orderInfo.address}}</view>
  </view>
  
  <view class="order-goods">
    <view class="h">
      <view :class="'checkbox ' + (checkedAllStatus ? 'checked' : '')" @tap="checkedAll">
        全选
      </view>
      <view class="label">选择退货的商品规格/数量</view>
      <view class="status">{{orderInfo.orderStatusText}}</view>
    </view>
    <view class="goods">
      <view v-for="(item, index) in orderGoods" :key="index" class="item">
        <view :class="'checkbox ' + (item.checked ? 'checked' : '')" @tap="checkedItem" :data-productId="item.productId">
        </view>
        <view class="img">
          <image :src="item.picUrl"></image>
        </view>
        <view class="info">
          <view class="t">
            <text class="name">{{item.goodsName}}</text>
          </view>
          <view class="t">
            <text class="attr">规格：{{item.specifications.toString()}}</text>
            <text class="number">x{{item.number}}</text>
          </view>
          <view class="price">￥{{item.price}}</view>
        </view>
      </view>
    </view>
  </view>
  
    <view class="order-bottom">
      <view class="pay-fee">
        <text class="label">退款总额：</text>
        <text class="txt">￥{{refundGoodsAmt}}</text>
      </view>
      <view class="input-box">
        <textarea class="content" focus="false" @input="bindInputValue" maxlength="160" placeholder="请联系客服确认后,录入退货(退款)申请理由,点击申请..." cursor-spacing="140"></textarea>
        <text class="count">{{160 - refundReason.length}}</text>
      </view>
    </view>
    <button class="refundApplyBnt" @tap="refundApply">{{orderInfo.orderStatusText == '已发货'? '退货申请':'退款申请'}}</button>
</view>
</template>

<script>
const util = require("../../../utils/util.js");
const api = require("../../../config/api.js");

export default {
  data() {
    return {
      orderId: 0,
      orderInfo: {},
      orderGoods: [],
      refundGoodsAmt: 0.0,
      refundReason: '',
      flag: false,
      handleOption: {},
      checkedAllStatus: true,
      isGrouponOrder: false
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.id
    });
    this.getOrderDetail();
  },

  onPullDownRefresh() {
    uni.showNavigationBarLoading(); //在标题栏中显示加载

    this.getOrderDetail();
    uni.hideNavigationBarLoading(); //完成停止加载

    uni.stopPullDownRefresh(); //停止下拉刷新
  },

  onReady: function () {// 页面渲染完成
  },
  onShow: function () {// 页面显示
  },
  onHide: function () {// 页面隐藏
  },
  onUnload: function () {// 页面关闭
  },
  methods: {
    getRefundGoodsAmt: function () {
      let that = this;
      let refundGoodsAmt = 0.00;
      that.orderGoods.forEach(function (v) {
        if (v.checked === true) {
          refundGoodsAmt = refundGoodsAmt + v.price * v.number;
        }
      });

      if (that.isGrouponOrder) {
        //如果是团购
        refundGoodsAmt = refundGoodsAmt - that.orderInfo.discountPrice;
      }

      let checkedAllStatus = this.isCheckedAll();

      if ((that.orderInfo.orderStatus == 201 || that.orderInfo.orderStatus == 204 || that.orderInfo.orderStatus == 205) && checkedAllStatus) {
        //订单未发货，且是全选，则还需退运费
        refundGoodsAmt = refundGoodsAmt + that.orderInfo.freightPrice;
      }

      refundGoodsAmt = new Number(refundGoodsAmt).toFixed(2);
      console.log(refundGoodsAmt);
      return refundGoodsAmt;
    },
    checkedAll: function () {
      if (this.isGrouponOrder) {
        //团购订单禁止退款部分商品
        util.showErrorToast('该订单属于团购，不支持选择退部分商品!');
        return false;
      } //编辑状态,将所有


      let that = this;
      let checkedAllStatus = this.isCheckedAll();
      let tmpOrderGoods = this.orderGoods.map(function (element, index, array) {
        element.checked = !checkedAllStatus;
        return element;
      });
      that.setData({
        orderGoods: tmpOrderGoods,
        checkedAllStatus: that.isCheckedAll(),
        refundGoodsAmt: that.getRefundGoodsAmt()
      });
    },
    isCheckedAll: function () {
      let that = this; //判断退款商品是否已全选

      return that.orderGoods.every(function (element, index, array) {
        if (element.checked == true) {
          return true;
        } else {
          return false;
        }
      });
    },
    expandDetail: function () {
      let that = this;
      this.setData({
        flag: !that.flag
      });
    },
    getOrderDetail: function () {
      uni.showLoading({
        title: '加载中'
      });
      setTimeout(function () {
        uni.hideLoading();
      }, 2000);
      let that = this;
      util.request(api.PreOrderRefund, {
        orderId: that.orderId
      }).then(function (res) {
        if (res.errno === 0) {
          console.log(res.data); //默认选中所有商品都选中

          let tmpOrderGoods = res.data.orderGoods.map(function (element, index, array) {
            element.checked = true;
            return element;
          });
          that.setData({
            orderInfo: res.data.orderInfo,
            isGrouponOrder: res.data.isGrouponOrder,
            orderGoods: tmpOrderGoods,
            checkedAllStatus: that.isCheckedAll(),
            handleOption: res.data.orderInfo.handleOption
          }); //设置商品信息后再获取退款金额

          let refundGoodsAmt = that.getRefundGoodsAmt();
          that.setData({
            refundGoodsAmt: refundGoodsAmt
          });
        }

        uni.hideLoading();
      });
    },
    checkedItem: function (event) {
      if (this.isGrouponOrder) {
        //团购订单禁止退款部分商品
        util.showErrorToast('该订单属于团购，不支持选择退部分商品!');
        return false;
      }

      let productId = event.currentTarget.dataset.productid;
      let that = this;
      let tmpGoodsData = this.orderGoods.map(function (element, index, array) {
        if (element.productId === productId) {
          element.checked = !element.checked;
        }

        return element;
      });
      that.setData({
        orderGoods: tmpGoodsData,
        checkedAllStatus: that.isCheckedAll(),
        refundGoodsAmt: that.getRefundGoodsAmt()
      });
    },
    // “取消订单并退款”点击效果
    refundOrder: function () {
      let that = this;
      let orderInfo = that.orderInfo;
      uni.showModal({
        title: '',
        content: '确定要取消此订单？',
        success: function (res) {
          if (res.confirm) {
            util.request(api.OrderRefund, {
              orderId: orderInfo.id
            }, 'POST').then(function (res) {
              if (res.errno === 0) {
                uni.showToast({
                  title: '取消订单成功'
                });
                util.redirect('/pages/ucenter/order/order');
              } else {
                util.showErrorToast(res.errmsg);
              }
            });
          }
        }
      });
    },

    bindInputValue(event) {
      let value = event.detail.value; //判断是否超过160个字符

      if (value && value.length > 160) {
        return false;
      }

      this.setData({
        refundReason: event.detail.value
      });
    },

    refundApply: function () {
      //获取已选择的商品
      let that = this;
      let productIds = [];
      that.orderGoods.forEach(function (o) {
        if (o.checked == true) {
          productIds.push(o.productId);
        }
      });

      if (productIds.length <= 0) {
        util.showErrorToast('请勾选需要退货的商品');
        return false;
      }

      if (!that.refundReason) {
        util.showErrorToast('请填写退货原因');
        return false;
      }

      util.request(api.RefundApply, {
        orderId: that.orderId,
        productIds: productIds,
        refundGoodsAmt: that.refundGoodsAmt,
        refundReason: that.refundReason
      }, 'POST').then(function (res) {
        if (res.errno === 0) {
          let refundId = res.data.refundId;
          uni.redirectTo({
            url: '/pages/ucenter/refundDetail/refundDetail?id=' + refundId
          });
        }
      });
    }
  }
};
</script>
<style>
page {
  height: 100%;
  width: 100%;
  background: #f4f4f4;
}

.order-info {
  padding-top: 25rpx;
  background: #fff;
  height: auto;
  padding-left: 31.25rpx;
  overflow: hidden;
}

.item-a {
  padding-left: 31.25rpx;
  height: 42.5rpx;
  padding-bottom: 12.5rpx;
  line-height: 30rpx;
  font-size: 30rpx;
  color: #666;
}

.item-b {
  padding-left: 31.25rpx;
  height: 29rpx;
  line-height: 29rpx;
  margin-top: 12.5rpx;
  margin-bottom: 41.5rpx;
  font-size: 30rpx;
  color: #666;
}

.item-c {
  margin-left: 31.25rpx;
  border-top: 1px solid #f4f4f4;
  height: 103rpx;
  line-height: 103rpx;
}

.item-c .l {
  float: left;
}

.item-c .r {
  height: 103rpx;
  float: right;
  display: flex;
  align-items: center;
  padding-right: 16rpx;
}

.item-c .r .btn {
  float: right;
}

.item-c .cost {
  color: #b4282d;
}

.item-c .btn {
  line-height: 66rpx;
  border-radius: 5rpx;
  text-align: center;
  margin: 0 15rpx;
  padding: 0 20rpx;
  height: 66rpx;
}

.item-c .btn.active {
  background: #b4282d;
  color: #fff;
}

.order-goods {
  margin-top: 20rpx;
  background: #fff;
}

.order-goods .h {
  height: 93.75rpx;
  line-height: 93.75rpx;
  margin-left: 31.25rpx;
  border-bottom: 1px solid #f4f4f4;
  padding-right: 31.25rpx;
}

.order-goods .h .label {
	float: left;
	font-size: 30rpx;
	color: #333;
	font-weight: bold;
}

.order-goods .h .status {
	float: right;
	font-size: 28rpx;
	color: #FD4D36;
}

.order-goods .item {
  display: flex;
  align-items: top;
  height: 202rpx;
  margin-bottom: 15.25rpx;
  padding-right: 31.25rpx;
  border-bottom: 1px solid #f4f4f4;
}

.order-goods .item:last-child {
  border-bottom: none;
}

.order-goods .item .img {
  height: 145.83rpx;
  margin-top: 15.5rpx;
  width: 145.83rpx;
  background: #f4f4f4;
}

.order-goods .item .img image {
  height: 145.83rpx;
  width: 145.83rpx;
}

.order-goods .item .info {
  flex: 1;
  height: 145.83rpx;
  margin-left: 20rpx;
}

.order-goods .item .t {
  margin-top: 14rpx;
  height: 45rpx;
  line-height: 45rpx;
  margin-bottom: 8.5rpx;
}

.order-goods .item .t .name {
  display: block;
  float: left;
  height: 33rpx;
  line-height: 33rpx;
  color: #333;
  font-size: 30rpx;
}

.order-goods .item .t .number {
  display: block;
  float: right;
  height: 29rpx;
  text-align: right;
  line-height: 29rpx;
  color: #333;
  font-size: 30rpx;
}

.order-goods .item .attr {
  height: 29rpx;
  line-height: 29rpx;
  color: #666;
  margin-bottom: 25rpx;
  font-size: 25rpx;
}

.order-goods .item .price {
  display: block;
  float: left;
  height: 30rpx;
  line-height: 30rpx;
  color: #333;
  font-size: 30rpx;
}

.order-goods .item .btn {
  height: 50rpx;
  line-height: 50rpx;
  border-radius: 40rpx;
  text-align: center;
  display: block;
  float: right;
  margin: 0 15rpx;
  padding: 0 20rpx;
}

.order-goods .item .btn.active {
  background: #ee9e09;
  color: #fff;
  border-radius: 40rpx;
}

.order-bottom {
  margin-top: 20rpx;
  padding-left: 31.25rpx;
  height: auto;
  overflow: hidden;
  background: #fff;
}

.order-info .address {
  height: 128rpx;
  padding-top: 25rpx;
}

.order-info .t {
  height: 60rpx;
  line-height: 55rpx;
  margin-bottom: 7.5rpx;
}

.order-info .lable {
  display: inline-block;
  height: 35rpx;
  width: 220rpx;
  line-height: 35rpx;
  font-size: 30rpx;
}

.order-info .value {
  display: inline-block;
  height: 35rpx;
  line-height: 35rpx;
  font-size: 30rpx;
  color: #999;
}

.order-info .b {
  height: 50rpx;
  line-height: 40rpx;
  font-size: 30rpx;
  color: #999;
  padding-right: 10rpx;
  margin-bottom: 40rpx;
}

.order-bottom .total {
  height: 136rpx;
  padding-top: 20rpx;
  border-bottom: 1px solid #f4f4f4;
}

.order-bottom .total .t {
  height: 30rpx;
  line-height: 30rpx;
  margin-bottom: 7.5rpx;
}

.order-bottom .total .label {
  width: 250rpx;
  height: 35rpx;
  color: #999;
  line-height: 35rpx;
  font-size: 30rpx;
  float: left;
}

.order-bottom .total .txt {
  height: 35rpx;
  float: right;
  padding-right: 31.25rpx;
  line-height: 35rpx;
  font-size: 30rpx;
}

.order-bottom .pay-fee {
  height: 81rpx;
  line-height: 81rpx;
}

.order-bottom .pay-fee .label {
  width: 250rpx;
  color: #333;
  font-weight: bold;
  float: left;
}

.order-bottom .pay-fee .txt {
  float: right;
  padding-right: 31.25rpx;
  color: #b4282d;
}

.order-express {
  margin-top: 20rpx;
  width: 100%;
  height: 100rpx;
  background: #fff;
}

.order-express .title {
  float: left;
  margin-bottom: 20rpx;
  padding: 20rpx;
}

.order-express .ti {
  float: right;
  width: 52rpx;
  height: 52rpx;
  margin-right: 16rpx;
  margin-top: 28rpx;
}

.order-express .t {
  font-size: 29rpx;
  margin-left: 10.25rpx;
  color: #a78845;
}

.order-express .b {
  font-size: 29rpx;
  margin-left: 10.25rpx;
  color: #a78845;
}

.order-express .traces {
  padding: 17.5rpx;
  background: #fff;
  border-bottom: 1rpx solid #f1e6cdcc;
}

.order-express .trace {
  padding-bottom: 17.5rpx;
  padding-top: 17.5rpx;
  background: #fff;
}

.order-express .acceptTime {
  margin-top: 20rpx;
  margin-right: 40rpx;
  text-align: right;
  font-size: 26rpx;
}

.order-express .acceptStation {
  font-size: 26rpx;
}

.order-goods .h .checkbox {
  float: left;
  padding-left: 50rpx;
  width:150rpx;
  height: 93.75rpx;
  background: url('') no-repeat 0rpx 30rpx;
  background-size: 34rpx;
  font-size: 29rpx;
}

.order-goods .h .checkbox.checked {
  width:150rpx;
  padding-left: 50rpx;
  background: url('') no-repeat 0rpx 30rpx;
  background-size: 34rpx;
}

.order-goods .item .checkbox {
  float: left;
  height: 34rpx;
  width: 34rpx;
  margin: 65rpx 18rpx 65rpx 26rpx;
  background: url('') no-repeat;
  background-size: 34rpx;
}

.order-goods .item .checkbox.checked {
  background: url('') no-repeat;
  background-size: 34rpx;
}
.order-bottom .input-box {
  height: 230rpx;
  width: 690rpx;
  position: relative;
  background: #fff;
}

.order-bottom .input-box .content {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  background: #fff;
  font-size: 29rpx;
  border: 5px solid #f4f4f4;
  height: 150rpx;
  width: 650rpx;
  padding: 20rpx;
}

.order-bottom .input-box .count {
  position: absolute;
  bottom: 33rpx;
  right: 20rpx;
  display: block;
  height: 30rpx;
  width: 50rpx;
  font-size: 29rpx;
  color: #999;
}
.refundApplyBnt {background-color:#FF654B;font-size:32rpx;color:#fff;width:690rpx;height:90rpx;border-radius:50rpx;text-align:center;line-height:90rpx;margin:26rpx auto 0 auto;}

</style>